package cn.diancan.util;


import org.junit.Test;


public class BaiduOffset {

	/**google转百度
	 * @param lat
	 * @param lng
	 * @return
	 */
	public static LocationCoordinate getGoogle2Baidu(String lat, String lng) {
		try{
			   //0表示原始GPS坐标，2表示Google
			String get_url = "http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=" + lng + "&y=" + lat + "";
			   String s=   HttpClientUtil.getStringByUrl2(get_url,false);
			   String sp[]= s.split(",");
			   BaiduBean bb= new BaiduBean();
			   bb.setError(Integer.parseInt(sp[0].split(":")[1]));
			   
			  //"error":0
			  //"x":"MTEyLjE0NjQwOTgwMDk0"
			  //"y":"MzIuMDE1MDgzMTA2MTgy"
			  //JSONObject js= JSONObject.fromObject(s);
			  // BaiduBean bb= (BaiduBean) JSONObject.toBean(js,BaiduBean.class);
			   LocationCoordinate l = new LocationCoordinate();
	 
			   if(bb!=null && bb.getError()==0){
				   //手动解析
				   String x =sp[1].split(":")[1];
				   bb.setX(x.substring(1,x.length()-1));
				   String y =sp[2].split(":")[1];
				   bb.setY(y.substring(1,y.length()-2));
				//   System.out.println(bb.getX() +"    "+bb.getY());
				   l.setLatitude( new String(SecBase64.decode(bb.getY())).trim() );
				   l.setLongitude(new String(SecBase64.decode(bb.getX())).trim());
				   return l;
			   }
				/**
				 117.31606273376
				 31.881362818653
				*/
		}catch(Exception e){
			
		}
		return null;
	}
	//"121.129616","41.115332" google
	//121.1355401074,41.122087774791 baidu
	//"32.009425","112.139833"
	//32.015083106182,112.14640980094
	public static String get(){
		return "";
	}
	
	
	@Test
	public void test(){//112.139833,32.009425
		LocationCoordinate l=BaiduOffset.getGoogle2Baidu("0", "0");
		System.out.println(l.toString());
	}
	
	public static void main(String[] args) {
		LocationCoordinate l = getGoogle2Baidu("32.009425","112.139833");
		System.out.println(l.toString());
	}

 
}
